{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "No module named 'tensorboardX'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-55149edd0f5e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mopts\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mopts\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mmodels\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodel\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mcreate_model\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mload_model\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msave_model\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     13\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mmodels\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata_parallel\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mDataParallel\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mlogger\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mLogger\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/lbc/shufflenet-centernet/CenterNet/src/lib/models/model.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[0;31m#from .networks.resnet_dcn import get_pose_net as get_pose_net_dcn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mnetworks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlarge_hourglass\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mget_large_hourglass_net\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mnetworks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshufflenet\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mget_shuffle_net\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     17\u001b[0m _model_factory = {\n",
      "\u001b[0;32m~/lbc/shufflenet-centernet/CenterNet/src/lib/models/networks/shufflenet.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mcollections\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mOrderedDict\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnn\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0minit\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorboardX\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mSummaryWriter\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     13\u001b[0m \u001b[0;31m#from torchviz import make_dot, make_dot_from_trace\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mImportError\u001b[0m: No module named 'tensorboardX'"
     ]
    }
   ],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import _init_paths\n",
    "\n",
    "import os\n",
    "\n",
    "import torch\n",
    "import torch.utils.data\n",
    "from opts import opts\n",
    "from models.model import create_model, load_model, save_model\n",
    "from models.data_parallel import DataParallel\n",
    "from logger import Logger\n",
    "from datasets.dataset_factory import get_dataset\n",
    "from trains.train_factory import train_factory\n",
    "#from tensorboardX import SummaryWriter\n",
    "#from torch.utils.tensorboard import SummaryWriter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#def main(opt):\n",
    "  torch.manual_seed(opt.seed)\n",
    "  torch.backends.cudnn.benchmark = not opt.not_cuda_benchmark and not opt.test\n",
    "  Dataset = get_dataset(opt.dataset, opt.task)\n",
    "  opt = opts().update_dataset_info_and_set_heads(opt, Dataset)\n",
    "  print(opt)\n",
    "\n",
    "  logger = Logger(opt)\n",
    "\n",
    "  os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus_str\n",
    "  opt.device = torch.device('cuda' if opt.gpus[0] >= 0 else 'cpu')\n",
    "  \n",
    "  print('Creating model...')\n",
    "  model = create_model(opt.arch, opt.heads, opt.head_conv)\n",
    "  optimizer = torch.optim.Adam(model.parameters(), opt.lr)\n",
    "  start_epoch = 0\n",
    "  if opt.load_model != '':\n",
    "    model, optimizer, start_epoch = load_model(\n",
    "      model, opt.load_model, optimizer, opt.resume, opt.lr, opt.lr_step)\n",
    "\n",
    "  #import pdb;pdb.set_trace()\n",
    "\n",
    "\n",
    "  Trainer = train_factory[opt.task]\n",
    "  trainer = Trainer(opt, model, optimizer)\n",
    "  trainer.set_device(opt.gpus, opt.chunk_sizes, opt.device)\n",
    "\n",
    "  print('Setting up data...')\n",
    "  val_loader = torch.utils.data.DataLoader(\n",
    "      Dataset(opt, 'val'), \n",
    "      batch_size=1, \n",
    "      shuffle=False,\n",
    "      num_workers=1,\n",
    "      pin_memory=True\n",
    "  )\n",
    "\n",
    "  if opt.test:\n",
    "    _, preds = trainer.val(0, val_loader)\n",
    "    val_loader.dataset.run_eval(preds, opt.save_dir)\n",
    "    return\n",
    "\n",
    "  train_loader = torch.utils.data.DataLoader(\n",
    "      Dataset(opt, 'train'), \n",
    "      batch_size=opt.batch_size, \n",
    "      shuffle=True,\n",
    "      num_workers=opt.num_workers,\n",
    "      pin_memory=True,\n",
    "      drop_last=True\n",
    "  )\n",
    "  #tensor board visulization\n",
    "  #import pdb; pdb.set_trace()\n",
    "  #writer = SummaryWriter('./Result')\n",
    "  #images, labels = next(iter(train_loader))\n",
    "  #grid = torchvision.utils.make_grid(images)\n",
    "  #writer.add_image('images', grid, 0)\n",
    "  #writer.add_graph(model, torch.rand([10,3,512,512]))\n",
    "  #writer.close()\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "  print('Starting training...')\n",
    "  best = 1e10\n",
    "  for epoch in range(start_epoch + 1, opt.num_epochs + 1):\n",
    "    mark = epoch if opt.save_all else 'last'\n",
    "    #import pdb\n",
    "    #pdb.set_trace()\n",
    "    log_dict_train, _ = trainer.train(epoch, train_loader)\n",
    "    logger.write('epoch: {} |'.format(epoch))\n",
    "    for k, v in log_dict_train.items():\n",
    "      logger.scalar_summary('train_{}'.format(k), v, epoch)\n",
    "      logger.write('{} {:8f} | '.format(k, v))\n",
    "    if opt.val_intervals > 0 and epoch % opt.val_intervals == 0:\n",
    "      save_model(os.path.join(opt.save_dir, 'model_{}.pth'.format(mark)), \n",
    "                 epoch, model, optimizer)\n",
    "      with torch.no_grad():\n",
    "        log_dict_val, preds = trainer.val(epoch, val_loader)\n",
    "      for k, v in log_dict_val.items():\n",
    "        logger.scalar_summary('val_{}'.format(k), v, epoch)\n",
    "        logger.write('{} {:8f} | '.format(k, v))\n",
    "      if log_dict_val[opt.metric] < best:\n",
    "        best = log_dict_val[opt.metric]\n",
    "        save_model(os.path.join(opt.save_dir, 'model_best.pth'), \n",
    "                   epoch, model)\n",
    "    else:\n",
    "      save_model(os.path.join(opt.save_dir, 'model_last.pth'), \n",
    "                 epoch, model, optimizer)\n",
    "    logger.write('\\n')\n",
    "    if epoch in opt.lr_step:\n",
    "      save_model(os.path.join(opt.save_dir, 'model_{}.pth'.format(epoch)), \n",
    "                 epoch, model, optimizer)\n",
    "      lr = opt.lr * (0.1 ** (opt.lr_step.index(epoch) + 1))\n",
    "      print('Drop LR to', lr)\n",
    "      for param_group in optimizer.param_groups:\n",
    "          param_group['lr'] = lr\n",
    "  logger.close()\n",
    "\n",
    "if __name__ == '__main__':\n",
    "  opt = opts().parse()\n",
    "  main(opt)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
